package com.watchthelan.web.server;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.watchthelan.service.*;
import com.watchthelan.service.server.ServerManager;
import com.watchthelan.domain.Server;

public class ServerAddFormController extends SimpleFormController {
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	private ServerManager serverManager;
	
	public ModelAndView onSubmit(Object command)
	throws ServletException {
		
		Server server = (Server) command;
		
		serverManager.addServer(server);

		logger.info("added server " + server);

		return new ModelAndView(new RedirectView(getSuccessView()));
	}

	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
		Server server = new Server();
		return server;
	}

	public ServerManager getServerManager() {
		return serverManager;
	}

	public void setServerManager(ServerManager serverManager) {
		this.serverManager = serverManager;
	}
}
